import dealHtml
import dealTxt
import downloadTs
import mergeTs
import urllib
import os
import shutil
import time

time_start=time.time()

print("Please input URL, for example: https://twitcasting.tv/natsuiromatsuri/movie/585300266")
print()
dealHtml.url = input("Your URL: ")

print("Downloading .html")
dealHtml.download_html(dealHtml.url)

download_from_url = dealHtml.m3u8_url(dealHtml.url).replace('\\','').replace('/fmp4','')

print("Downloading .m3u8")
urllib.request.urlretrieve(download_from_url,"temp/" + dealHtml.filename_m3u8(dealHtml.url))

print("Changing into .txt")
urllib.request.urlretrieve(download_from_url,"temp/" + dealHtml.filename_txt(dealHtml.url))

print("Deleting the-#-lines")
dealTxt.delete_lines()

print("Start to download ts-files")
downloadTs.download_ts_files(download_from_url)

print("Creating .bat")
mergeTs.create_bat()


os.chdir(os.getcwd() + "/temp/ts")
os.system(dealTxt.filename_edited_txt(dealHtml.url) + '.bat')
os.chdir(os.path.pardir)
os.chdir(os.path.pardir)
shutil.copyfile("temp/ts/new.ts","download.ts")
shutil.rmtree("temp",ignore_errors=True)

print("download.ts 已下载到当前目录")

time_end=time.time()
print("运行时间: ",time_end-time_start,"s")
print("平均下载速度: ",os.path.getsize("download.ts")/1024/(time_end-time_start)," KB/s")
print("按任意键退出")
a = input()